package com.pagoda.nerp.trade.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pagoda.nerp.trade.app.goods.dto.MdCategoryDto;
import com.pagoda.nerp.trade.common.PageListResult;
import com.pagoda.nerp.trade.dao.MdCategoryDao;
import com.pagoda.nerp.trade.dao.MdGoodsDao;
import com.pagoda.nerp.trade.entity.MdCategory;
import com.pagoda.nerp.trade.entity.MdGoods;
import com.pagoda.nerp.trade.service.MdCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MdCategoryImpl implements MdCategoryService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MdCategoryDao categoryDao;

    @Autowired
    private MdGoodsDao goodsDao;

    /**
     * 查询水果分类
     *
     * @param params 水果分类Id
     */
    @Override
    public PageListResult<MdCategoryDto> queryCategoryTree(Map<String,Object> params) throws Exception{
        // 1.参数处理
        int categoryId, pageNo, pageSize = 0;
        try {
            categoryId = Integer.parseInt(params.get("categoryId").toString());
            pageNo = null == params.get("pageNo") ? 0 : Integer.parseInt(params.get("pageNo").toString());
            pageSize = null == params.get("pageSize") ? 10 : Integer.parseInt(params.get("pageSize").toString());
        } catch (NumberFormatException e){
            throw new Exception("数字格式异常！");
        }

        // 2.分页处理
        Page pageInfo = PageHelper.startPage(pageNo, pageSize);

        // 3.查询
        List<MdCategory> mdCategoryList = categoryDao.selectByCategoryId(categoryId);

        // 4.判断结果集是否为空
        if (null == mdCategoryList){
            return null;
        }

        // 5.MdCategory转MdCategoryDto
        List<MdCategoryDto> categoryList = new ArrayList<MdCategoryDto>();
        MdCategoryDto category = null;
        for(MdCategory mgc:mdCategoryList){
            category = new MdCategoryDto();

            category.setCategoryId(mgc.getCategoryId());
            category.setCategoryName(mgc.getCategoryName());
            category.setParentCategoryId(mgc.getParentCategoryId());

            categoryList.add(category);
        }

        return new PageListResult<MdCategoryDto>(pageInfo, categoryList);
    }

    /**
     * 查询分类水果信息列表
     *
     * @param params 水果Id
     */
    @Override
    public PageListResult<Map<String,Object>> queryCategoryGoods(Map<String,Object> params) throws Exception{
        // 1.参数处理
        int categoryId, pageNo, pageSize = 0;
        try {
            categoryId = Integer.parseInt(params.get("categoryId").toString());
            pageNo = null == params.get("pageNo") ? 0 : Integer.parseInt(params.get("pageNo").toString());
            pageSize = null == params.get("pageSize") ? 10 : Integer.parseInt(params.get("pageSize").toString());
        } catch (NumberFormatException e){
            throw new Exception("数字格式异常！");
        }


        // 2.分页处理
        Page pageInfo = PageHelper.startPage(pageNo, pageSize);

        // 3.查询
        List<MdGoods> mdGoodsList = goodsDao.selectByCategoryId(categoryId);

        // 4.判断结果集是否为空
        if (null == mdGoodsList){
            return null;
        }

        // 5.MdGoods转Map
        List<Map<String,Object>> goodsList = new ArrayList<Map<String,Object>>();
        Map<String,Object> good = null;
        for(MdGoods md : mdGoodsList){
            good = new HashMap<String,Object>();

            good.put("goodsId", md.getGoodsId());
            good.put("goodsName", md.getGoodsName());
            good.put("categoryId", md.getCategoryId());

            goodsList.add(good);
        }

        return new PageListResult<Map<String,Object>>(pageInfo, goodsList);
    }
}
